package main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import dao.PropertyIO;

import sample_schedular.NegativeRandomSampler;
import sample_schedular.PositiveRandomSampler;
import sample_schedular.RandomSampler;
import sample_schedular.Sampler;

public class RandomSampling {

	public void proceed(Properties properties) {
		String filename = properties.getProperty("input_file");
		String outputfile = properties.getProperty("output_file");
		int n = Integer.parseInt(properties.getProperty("resample_size"));
		String sampleMethod = properties.getProperty("sample_method");
		
		proceed(filename, outputfile, n, sampleMethod);
	}

	public void proceed(String filename, String outputfile, int n, String sampleMethod) {

		Sampler sampler;
		if(sampleMethod.compareTo("negative_random") == 0) {
			sampler = new NegativeRandomSampler();
		} else if(sampleMethod.compareTo("random") == 0) {
			sampler = new RandomSampler();
		} else if(sampleMethod.compareTo("positive_random") == 0) {
			sampler = new PositiveRandomSampler();
		} else {
			throw new RuntimeException();
		}
		//sampler.initialize(properties);
		List<Integer> labels = getLabels(filename);
		List<Integer> picked = sampler.resample(labels, n);
		
		resample(filename, outputfile, picked);
	}
	
	private List<Integer> getLabels(String filename) {
		List<Integer> list = new LinkedList<Integer>();
		
		try {
			BufferedReader br = new BufferedReader(new FileReader(filename));

			while (br.ready()) {
				char label = (char)br.read();
				br.readLine();
				
				int l = label == '0' ? 0 : 1;
				list.add(l);
			}
			br.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return list;
	}
	
	private void resample(String inputfile, String outputfile, List<Integer> picked) {
		Set<Integer> set = new HashSet<Integer>(picked);
		try {
			BufferedReader br = new BufferedReader(new FileReader(inputfile));
			BufferedWriter bw = new BufferedWriter(new FileWriter(outputfile));

			int index = 0;
			while (br.ready()) {
				
				
				if (set.contains(index)) {
					String line = br.readLine();
					bw.append(line + "\n");
				} else {
					br.readLine();
				}
				index ++;
			}
			br.close();
			bw.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static void main(String[] args) {
		RandomSampling rs = new RandomSampling();
	
		if(args.length == 1) {
			Properties properties = PropertyIO.load(args[0]);
			rs.proceed(properties);
		} else if(args.length == 4){
			String filename = args[0];
			String outputfile = args[1];
			int n = Integer.parseInt(args[2]);
			String sampleMethod = args[3];
			rs.proceed(filename, outputfile, n, sampleMethod);
		}		
	}

}
